<?php

class Empeval_Methods_Request_SendRejection {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $bonusGoalsFormId;
	public $content;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->bonusGoalsFormId = $params->bonusGoalsFormId;
		$this->content = $params->content;
	}
	
	public function process() {
		$bonusGoalsForms = EmpevalViewBgfWithSuperiorPositionIdQuery::create()
		->useUserPositionsRelatedBySuperiorUserPositionIdQuery('SuperiorUserPosition')
			->joinUsers('Superior')
		->endUse()
		->useUserPositionsRelatedByUserPositionIdQuery()
			->filterByUsers(Empeval_Auth::getInstance()->getUser())
		->endUse()
		->filterById($this->bonusGoalsFormId)
		->with('SuperiorUserPosition')
		->with('Superior')
		->findOne();
		if (!($bonusGoalsForms instanceof EmpevalViewBgfWithSuperiorPositionId) || $bonusGoalsForms->getStatus() !== EmpevalBonusGoalsForms::STATUS_TO_USER_ACCEPT_3)
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		$con = Propel::getConnection('vgp_empeval');
		$con->beginTransaction();
		try {
			$rejection = new EmpevalRejections();
			$rejection->setBonusGoalsFormId($this->bonusGoalsFormId);
			$rejection->setText($this->content);
			$rejection->setStatus(0);
			$rejection->setActive(1);
			$rejection->save($con);
			EmpevalBonusGoalsFormsQuery::create()
				->filterById($this->bonusGoalsFormId)
				->update(array('Status' => EmpevalBonusGoalsForms::STATUS_REJECTED), $con);
				
			$notification = new Empeval_Notifications_Rejection();
			$notification->addNotifications(Empeval_Auth::getInstance()->getUser(), $bonusGoalsForms, $rejection);
			
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
			
		return new Empeval_Methods_Response_SendRejection(true, $rejection, $bonusGoalsForms->getStatus());
	}
	
}